﻿using commonImportPlugin.Configs.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace commonImportPlugin.Configs.SsseFile
{
    public class SsseFileConfigExcel : ConfigsBase,IcommonBase
    {
        //
        //    基本配置参数
        //
        #region
        /// <summary>
        /// 工厂配置
        /// </summary>
        public string factory { set; get; }
        /// <summary>
        /// 筛选数据文件规则
        /// (文件名正则[#]文件内容正则)
        /// </summary>
        public string fileFilterRules { set; get; }
        /// <summary>
        /// 社保数据的起始位置（1开始）
        /// </summary>
        public int dataIndex { set; get; }
        /// <summary>
        /// 社保银行卡号所处列
        /// </summary>
        public int[] subBankCardIdRules { set; get; }
        /// <summary>
        /// 社保身份证号所处列
        /// </summary>
        public int[] subPersonIdRules { set; get; }
        /// <summary>
        /// 社保卡号所处列
        /// </summary>
        public int[] subCardIdRules { set; get; }
        /// <summary>
        /// 社保数据检查正则表达式
        /// </summary>
        public string dataCheckRegexs { set; get; }
        /// <summary>
        /// 该参数用于实现过滤之前已导入的数据
        /// 该参数值为：bankCardId,personId,ssCardId之一
        /// </summary>
        public string filterItem { set; get; }
        #endregion
        //
        //    其他
        //
        #region
        private static SsseFileConfigExcel uniqueInstance;
        public void Init()
        {
            SsseFileConfigExcel config = new SsseFileConfigExcel();
            string checkRegexs = @"^\d{1,}&(-\d{1,}|\d{1,})&(-\d{1,}|\d{1,})$";//检查参数配置的参数格式为：数字(+)&数字(-+)&数字(-+)

            //工厂配置
            config.factory = configs["SsseFileFactory"];

            //获取文件扩展名
            if (!IsKeyExist("SsseFileFilterRules"))
                throw new Exception(SsseErrors.SsseFileFilterRulesError);

            config.fileFilterRules = configs["SsseFileFilterRules"];

            //获取银行卡号
            if (!IsKeyEmpty("SsseFileSubBankCardId"))
            {
                if (!Regex.IsMatch(configs["SsseFileSubBankCardId"], checkRegexs))
                    throw new Exception(SsseErrors.SsseFileSubBankCardIdError);

                config.subBankCardIdRules = ToIntArrays(configs["SsseFileSubBankCardId"]);
            }

            //获取身份证号
            if (!IsKeyEmpty("SsseFileSubPersonId"))
            {
                if (!Regex.IsMatch(configs["SsseFileSubPersonId"], checkRegexs))
                    throw new Exception(SsseErrors.SsseFileSubPersonIdError);
                config.subPersonIdRules = ToIntArrays(configs["SsseFileSubPersonId"]);
            }

            //获取医保卡号
            if (!IsKeyEmpty("SsseFileSubCardId"))
            {
                if (!Regex.IsMatch(configs["SsseFileSubCardId"], checkRegexs))
                    throw new Exception(SsseErrors.SsseFileSubCardIdError);
                config.subCardIdRules = ToIntArrays(configs["SsseFileSubCardId"]);
            }

            //数据检查正则
            if (IsKeyExist("SsseFileDataCheckRegexs"))
            {
                config.dataCheckRegexs = configs["SsseFileDataCheckRegexs"];
            }

            //过滤数据项（1开始）
            if (!IsKeyEmpty("SsseFileFilterItem"))
            {

                string[] items = new string[] { "bankCardId", "personId", "ssCardId" };
                string item = configs["SsseFileFilterItem"].Trim();

                if (!items.Contains(item))
                    throw new Exception(SsseErrors.SsseFileFilterItemError);

                config.filterItem = configs["SsseFileFilterItem"];
            }

            uniqueInstance=config;
        }
        /// <summary>
        /// 获取配置
        /// </summary>
        /// <returns></returns>
        public static SsseFileConfigExcel GetConfig()
        {
            if (uniqueInstance == null)
            {
                return null;
            }
            return uniqueInstance;
        }
        #endregion
    }  
}
